<?php
/**
 * Класс обработки AJAX запросов
 * @author mrpie
 */
class modAJAX extends BasicAJAX {
	
	public function __construct(Core $Site) {
		parent::__construct($Site);
		$this->AjaxQuerys = array(
			'get_cityid'	=> 'getCityId',
			'get_informer' => 'getInformerCode'
		);
	}
	public function CallFunc() {
		if (isset($this->AjaxQuerys[$this->URL->Params['ajax']])) {
			if (is_callable(array($this, $this->AjaxQuerys[$this->URL->Params['ajax']]))) {
				call_user_func_array(array($this, $this->AjaxQuerys[$this->URL->Params['ajax']]), array());
			} else {$this->RequestArr['ajaxMsg'] = 'Не найден обработчик AJAX запроса';}
		} else {$this->RequestArr['ajaxMsg'] = 'Не найден обработчик AJAX запроса';}
	}
	private function getCityId() {
		$Select = $this->MySQL->Select('SELECT `c`.`ID`, `c`.`NAME`, `a`.`NAME` `A_NAME`, `d`.`NAME` `D_NAME` FROM `{p}cities` `c` INNER JOIN `{p}areas` `a` ON `a`.`ID` = `c`.`AREA_ID` INNER JOIN `{p}districts` `d` ON `d`.`ID` = `c`.`DISTRICT_ID` WHERE `c`.`NAME` LIKE "'.$this->URL->Params['name'].'%"', MySQL::SQuery);
		while (!is_null($City = $this->MySQL->FetchArray($Select))) {
			$this->RequestArr['cities'][] = array(
				'ID'	=> $City['ID'],
				'NAME'	=> $City['NAME'].', '.$City['A_NAME'].', '.$City['D_NAME']
			);
		}
		$this->RequestArr['ajaxRez'] = 'true';
		return 0;
	}
	private function getInformerCode() {
		$charsS = array('a','b','d','e','f','h','i','k','n','r','s','t','y','z');
		$charsB = array('A','B','D','E','F','G','H','K','N','Q','R','S','T','Y','Z');
		$charsN = array('1','2','3','4','5','6','7','8','9');
		$ParamsArray = array();
		foreach ($this->URL->Params as $Index => $Value) {
			if ($Index != 'q' && $Index != 'ajax') {$ParamsArray[$Index] = $Value;}
		}
		$Name = '';
		$Params	= addslashes(json_encode($ParamsArray));
		$MD5 = md5($Params);
		$this->RequestArr['ajaxRez'] = 'true';
		$Select = $this->MySQL->Select('SELECT `ID` FROM `{p}informers` WHERE `MD5` = "'.$MD5.'"');
		if ($Select) {
			$Name = $Select['ID'];
		} else{
			$Name = '';
			for ($i = 0; $i < 10; ++$i) {
				$r = rand(1, 3);
				if ($r == 1) {$Name .= $charsS[rand(0, 14)];}
				elseif ($r == 2) {$Name .= $charsB[rand(0, 14)];}
				else {$Name .= $charsN[rand(0, 10)];}
			}
			$Query = $this->MySQL->Query('INSERT INTO `{p}informers` (`ID`, `MD5`, `PARAMS`) VALUES ("'.$Name.'", "'.$MD5.'", "'.$Params.'")');
			if (!$Query) {$this->RequestArr['ajaxRez'] = 'false';var_dump($this->MySQL->LastSQL());}
		}
		$Select = $this->MySQL->Select('SELECT `URL`, `NAME` FROM `{p}cities` `c` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `c`.`ID` AND `URL_TYPE` = 3 WHERE `c`.`ID` = '.$this->URL->Params['city_id']);
		$this->RequestArr['code'] = '<!-- weather.by informer -->
<link rel="stylesheet" type="text/css" href="http://www.weather.by/css/'.strtr($this->URL->Params['sel_inf'], array('#' => '')).'/weathInf.style.css?id='.$Name.'">
<div id="weathInfID-'.$Name.'">
	<a href="http://www.weather.by'.$Select['URL'].'" target="_blank">Погода в '.$Select['NAME'].'</a>
	<a href="http://www.weather.by/" target="_blank"><img alt="weather.by" title="weather.by" src="http://www.weather.by/images/weather_logo.png" border="0" /></a>
</div>
<script src="http://www.weather.by/informer/inf.js?id='.$MD5.'" type="text/javascript"></script>
<!-- // -->';
		return 0;
	}
}
?>